package com.topvision.elementanim.view;

import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

import com.topvision.elementanim.R;

/**
 * Author: lucy
 * Comment: //TODO
 * Date: 2017-09-30 16:26
 */

public class CustomEditText extends EditText {

    private static final String TAG = "CustomEditText";
    private Paint mPaint;
    private ObjectAnimator lineAnimator;
    private int mMeasuredWidth;
    private int mMeasuredHeight;
    private int length;
    private float mViewHeight;
    private int state;
    private int mLineAnimWidth;

    private final int DEFAULT_PAINT_COLOR = R.color.colorAccent;
    private int CURRENT_PAINT_COLOR = DEFAULT_PAINT_COLOR;
    private int SELECT_PAINT_COLOR = DEFAULT_PAINT_COLOR;
    private int animColor = DEFAULT_PAINT_COLOR;

    private int DEFUALT_ANIM_DURATION = 800;
    private int animDuration = DEFUALT_ANIM_DURATION;

    private int DEFUALT_STROKE_WIDTH = 5;

    public CustomEditText(Context context) {
        super(context, null);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setBackground(null);
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setColor(getResources().getColor(DEFAULT_PAINT_COLOR));
        mPaint.setStrokeWidth(DEFUALT_STROKE_WIDTH);
    }
    public CustomEditText setUnderLineStrokeWidth(int strokeWidth) {
        getLinePaint().setStrokeWidth(strokeWidth);
        return this;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mMeasuredWidth = getMeasuredWidth();
        mMeasuredHeight = getMeasuredHeight();
        mLineAnimWidth = mMeasuredWidth / 2;
        mViewHeight = 9f / 10 * mMeasuredHeight;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (state) {
            case 0:
                canvas.drawLine(0, mViewHeight, mMeasuredWidth, mViewHeight, mPaint);
                break;
            case 1:
                canvas.drawLine(mLineAnimWidth, mViewHeight, mLineAnimWidth - length, mViewHeight, mPaint);
                canvas.drawLine(mLineAnimWidth, mViewHeight, mLineAnimWidth + length, mViewHeight, mPaint);
                break;
        }
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        state = 0;
        if (focused) {
            drawLineAnim(animColor);
        } else {
            try {
                getLinePaint().setColor(getResources().getColor(CURRENT_PAINT_COLOR));
            } catch (Exception e) {
                getLinePaint().setColor(CURRENT_PAINT_COLOR);
            }

        }
        invalidate();
    }

    private Paint getLinePaint() {
        if (null == mPaint)
            initPaint();
        return mPaint;
    }

    /* 一些外部调用的方法 */
    public CustomEditText setDefaultUnderLineColor(int color) {
        if (color != 0) {
            getLinePaint().setColor(color);
            CURRENT_PAINT_COLOR = color;
        } else {
            getLinePaint().setColor(getResources().getColor(DEFAULT_PAINT_COLOR));
            CURRENT_PAINT_COLOR = DEFAULT_PAINT_COLOR;
        }
        return this;
    }

    public CustomEditText setDefaultUnderLineColor(String color) {
        if (null != color) {
            setDefaultUnderLineColor(Color.parseColor(color));
        } else {
            setDefaultUnderLineColor(0);
        }
        return this;
    }

    public CustomEditText setAnimColor(int color) {
        this.animColor = color;
        return this;
    }

    public CustomEditText setAnimColor(String color) {
        if (color != null)
            setAnimColor(Color.parseColor(color));
        else
            setAnimColor(0);
        return this;
    }

    private void drawLineAnim(int color) {
        if (color != 0) {
            getLinePaint().setColor(color);
            SELECT_PAINT_COLOR = color;
        } else {
            SELECT_PAINT_COLOR = DEFAULT_PAINT_COLOR;
        }
        lineAnimator = ObjectAnimator.ofInt(this, "line", 0, 0);
        lineAnimator.setIntValues(0, mLineAnimWidth);
        lineAnimator.setEvaluator(new IntEvaluator());
        lineAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                length = (int) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });
        if (!lineAnimator.isStarted()) {
            state = 1;
            lineAnimator.setDuration(animDuration);
            lineAnimator.start();
        }
    }

    public CustomEditText setAnimDuration(int duration) {
        this.animDuration = duration;
        return this;
    }
}
